package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.entity.Shop_cart_product;
import com.aigou.admin.service.Shop_cart_productService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: May
 * @Date: 2024/12/3 17:15
 * @Version: 1.0
 */

@RestController
@RequestMapping("/shop_cart_product")
@AllArgsConstructor
@ResponseResult
public class Shop_cart_productController {

    private final Shop_cart_productService shopCartProductService;

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String shop_cart_id = req.getParameter("shop_cart_id");
        String product_id = req.getParameter("product_id");
        String sales_num = req.getParameter("product_num");
        Shop_cart_product shopCartProduct = new Shop_cart_product(id,
                shop_cart_id,
                product_id,
                Integer.valueOf(sales_num));
        Boolean save = shopCartProductService.save(shopCartProduct);
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获得id
        String id = req.getParameter("id");
        // 查询商品
        Shop_cart_product sales = shopCartProductService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(sales);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        List<Shop_cart_product> sales = shopCartProductService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(sales);
    }

    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean remove = shopCartProductService.remove(id);
        return ResponseVo.buildSuccess(remove);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String shop_cart_id = req.getParameter("shop_cart_id");
        String product_id = req.getParameter("product_id");
        String sales_num = req.getParameter("product_num");
        Shop_cart_product sales = new Shop_cart_product(id,
                shop_cart_id,
                product_id,
                Integer.valueOf(sales_num));
        Boolean update = shopCartProductService.update(sales);
        return ResponseVo.buildSuccess(update);
    }

    /**
     * 分页查询
     *
     * @param req
     * @param resp
     * @return
     * @throws IOException
     */
    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
       //查询用户
        PageEntity<Shop_cart_product> shopCartProductsByPage = shopCartProductService.findUserPage(Integer.parseInt(current), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(shopCartProductsByPage);
    }


}
